package com.ph.monitorPlatform.service.impl;

import com.alibaba.excel.EasyExcel;
import com.ph.monitorPlatform.dto.AlertExportDto;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Service
public class ExportToExcel {

    public void exportToExcel(HttpServletResponse response, List exportData, String excelFileName, Class c) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode(excelFileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), c).sheet("data").doWrite(exportData);
    }
}
